package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
	"gorm.io/gorm"
)

type FdJqService struct {
}

// CreateFdJq 创建节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) CreateFdJq(fdJq *health.FdJq) (err error) {
	err = global.GVA_DB.Create(fdJq).Error
	return err
}

// DeleteFdJq 删除节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) DeleteFdJq(fdJq health.FdJq) (err error) {
	err = global.GVA_DB.Delete(&fdJq).Error
	return err
}

// DeleteFdJqByIds 批量删除节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) DeleteFdJqByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.FdJq{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFdJq 更新节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) UpdateFdJq(fdJq health.FdJq) (err error) {
	err = global.GVA_DB.Save(&fdJq).Error
	return err
}

// GetFdJq 根据id获取节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) GetFdJq(id uint) (fdJq health.FdJq, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&fdJq).Error
	return
}

// GetFdJqInfoList 分页获取节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdJqService *FdJqService) GetFdJqInfoList(info healthReq.FdJqSearch) (list []health.FdJq, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.FdJq{})
	var fdJqs []health.FdJq
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdJqs).Error
	return fdJqs, total, err
}

func (fdJqService *FdJqService) SetFdJqGraphic(req healthReq.FdJqArticleReq) (err error) {
	var FdJqGraphic []healthReq.FdJqArticle
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		err = tx.Where("fd_jq_id = ?", req.FdJqID).Delete(&FdJqGraphic).Error
		if err != nil {
			return err
		}
		var jqArticles []healthReq.FdJqArticle
		for _, graphic := range req.Graphics {
			jqArticles = append(jqArticles, healthReq.FdJqArticle{
				FdJqID:    req.FdJqID,
				ArticleID: graphic,
				Type:      "graphic",
			})
		}
		for _, audio := range req.Audios {
			jqArticles = append(jqArticles, healthReq.FdJqArticle{
				FdJqID:    req.FdJqID,
				ArticleID: audio,
				Type:      "audio",
			})
		}
		for _, video := range req.Videos {
			jqArticles = append(jqArticles, healthReq.FdJqArticle{
				FdJqID:    req.FdJqID,
				ArticleID: video,
				Type:      "video",
			})
		}

		err = tx.Create(&jqArticles).Error
		return err
	})
}

func (fdJqService *FdJqService) GetFdJqGraphic(fd_jq_id string) (FdJqGraphic []healthReq.FdJqArticle, err error) {
	err = global.GVA_DB.Where("fd_jq_id = ?", fd_jq_id).Find(&FdJqGraphic).Error
	return
}
